import csv
import pr


def load_file(filename):
  reader = csv.reader(open(filename))

  consts = []

  i = 1
  region = None
  currentparties = []
  onregion = False

  for row in reader:
    if onregion == True:
      i = 0
        
    if row == ['','','','']:
      i = 0
      consts[len(consts)-1].set_results(currentparties)
      currentparties = []
        

    if i == 1:
      if row[1] != "Region":
        consts.append(pr.constituency(row[1],[]))
      else:
        if row[1] == "Region":
          region = pr.region(row[0], [], consts, 7)
          onregion = True
    if row[2] != '':
      currentparties.append(pr.party(row[2], int(row[3]), None))

    
    i += 1
#for i in currentparties:
#  print i.name
    
  region.set_results(currentparties)
  return region
#print "Region start\n"
#for j in region.get_elected():
#  print j.name
#print "Region End\n"

#print "Region start\n"
#for j in region.get_elected():
#  print j.name
#print "Region End\n"
   
#for i in consts:
#  print i.get_elected().name

#for i in region.constituencies:
#  print i.name + " - " + i.get_elected().name

#print "6% Swing SNP to Labour"
#region.constituency_uniform_swing(10, "Labour", "SNP")
#region.uniform_swing(1, "Labour", "SNP")
#for i in region.constituencies:
#  print i.name + " - " + i.get_elected().name
  
#for j in region.get_elected(7):
#  print j.name
#x = raw_input()

def load_index(filename):
  reader = csv.reader(open(filename))
  regions = []
  for row in reader:
    regions.append(load_file(row[2]))
  return regions
